#include "NetworkModel.h"

#include <QNetworkConfigurationManager>

#include <QDebug>

NetworkModel::NetworkModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_manager(new QNetworkConfigurationManager(this))
{
    QHash<int, QByteArray> role_names;
    role_names.insert(RoleName, "name");
    role_names.insert(RoleDescription, "description");
    setRoleNames(role_names);

    scan();
}

NetworkModel::~NetworkModel()
{

}

void NetworkModel::scan()
{
    m_configurations = m_manager->allConfigurations();
    reset();
}

int NetworkModel::rowCount(const QModelIndex &parent) const
{
    return m_configurations.count();
}

QVariant NetworkModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    QNetworkConfiguration configuration(m_configurations.at(index.row()));

    switch (role) {
        case RoleName:
            return configuration.name();
        case RoleDescription:
            return configuration.bearerTypeName();
        default:
            break;
    }

    return QVariant();
}

QNetworkConfiguration NetworkModel::configurationForRow(int row)
{
    return m_configurations.at(row);
}
